let machine = require('../public/index.js');
const { HotpotOrder, HotpotInFoodOrder, HotpotOutFoodOrder, HotpotPreFurnaceOrder } = require('../db/schema')
const { to, reverseTcpData, jsonCopy } = require('../utils/index')
const date = require('silly-datetime')
const { reqchangeFreezerStock, reqchangeThermotankStock, reqcheckQrCode, getThermotankTakePlace } = require('./request')
const config = require('../config/index.js');
const kafka = require('../utils/kafka-utils');

let EventEmitter = require('events').EventEmitter;
let event = new EventEmitter();

let today = date.format(new Date(), 'YYYYMMDD');


let inFoodObj = {
    "storeId": 1201818447324033025,   //门店Id
    "deviceId": 1204291689871810562,  //设备Id
    "thermotankNo": 1,   //保温柜编号
    "floor": 1,    //层
    "placeNo": 15,  //格子编号
    "productId": 11111111111111,   //菜品id
    "productCode": 1,   //菜品编码
    "placeStock": 0,   //有餐/无餐   0-无餐；1-有餐
    "entryTime": null  //进保温柜时间
}

let outFoodObj = {
    "storeId": 1201818447324033025,   //门店Id
    "deviceId": 1204291689871810562,  //设备Id
    "thermotankNo": 1,   //保温柜编号
    "floor": 1,    //层
    "placeNo": 14,  //格子编号
    "productId": 11111111111111,   //菜品id
    "productCode": 1,   //菜品编码
    "placeStock": 0,   //有餐/无餐   0-无餐；1-有餐
    "entryTime": null  //进保温柜时间
}

class hotPotRice extends machine {
    constructor(http, tcpServe, kafkaClient) {
        super(http, tcpServe, kafkaClient);

        this.kafkaClient = kafkaClient

        this.storeId = '1201818447324033025'
        this.deviceId = '1204291689871810562'
        this.productId1 = '1145957515463983112' //腊味煲仔饭
        this.productId2 = '1145957515463983111' //黑椒鸡柳煲仔饭

        this.devices = {}
        this.thermotanks = {}

        this.random = 1001

        this.machineInfos = [
            { header: '170', ip: '::ffff:127.0.0.1' }
        ]

        this.startOrderSwitch = true

        this.init()

        this.resendObj = {}
        // HotpotOutFoodOrder.findOne({}).then(res => {
        //     console.log(res)
        // }).catch(err => {
        //     console.log(err)
        // })


    }

    init() {
        this.listenKafka()
        this.listenTcpData()
        this.checkHeartbeat()
        this.prepareFood()
    }

    listenKafka() {
        let kafkaClient = new kafka(config.kafkaUrl(), [{ topic: 'yiyuan' }]);
        kafkaClient.message((message) => {
            console.log(message)
            let value = JSON.parse(message.value)
            //备餐命令
            if (value.instruction === 'COOK_CLAYPOT') {
                this.insertOrder(message.topic, value)

                //打烊清库存
            } else if (value.instruction === 'CLEAR_THERMOTANK_STOCK') {
                this.insertOutFoodOrder(value, 1)

                //超时清库存
            } else if (value.instruction === 'CLEAR_OVERTIME_PRODUCT') {
                this.insertOutFoodOrder(value, 2)
            }
        })
    }

    //插入备餐数据
    async insertOrder(topic, order) {
        let insertData = []

        let preFurnaceQuantity1 = 0
        let preFurnaceQuantity2 = 0

        order.data.forEach(item => {
            let orderDetail = jsonCopy(item)
            // 统计要预热的炉头
            if (item.productCode === 1) { preFurnaceQuantity1 += item.quantity }
            if (item.productCode === 2) { preFurnaceQuantity2 += item.quantity }
            orderDetail.quantity = 1
            for (let i = 0; i < item.quantity; i++) {
                insertData.push({
                    date: today,
                    orderDetail,
                    status: 0,
                    topic
                })
            }

        })

        let [error1, result1] = await to(HotpotOrder.insertMany(insertData))
        if (error1) return

        console.log("insert last mealId = " + (result1[result1.length - 1]._mealId - 1))

        //接收到订单数据之后预热炉头1
        if (preFurnaceQuantity1) {
            this.preFurnace(preFurnaceQuantity1, 1)
        }

        //接收到订单数据之后预热炉头2
        if (preFurnaceQuantity2) {
            this.preFurnace(preFurnaceQuantity2, 2)
        }

    }

    //预热炉头
    async preFurnace(quantity, preFurnaceId) {
        let insertData = [{
            date: today,
            quantity: quantity,
            status: 0,
        }]
        let [error, result] = await to(HotpotPreFurnaceOrder.insertMany(insertData))
        if (error) return
        let _preFurnaceOrderId = result[0]._preFurnaceOrderId
        console.log("insert last _preFurnaceOrderId = " + _preFurnaceOrderId)

        let content = this.messageResponse(170, 11, this.random, 6, _preFurnaceOrderId, preFurnaceId, preFurnaceQuantity2)
        let machineInfo = this.machineInfos.find(item => item.header === '170')
        if (!this.clients[machineInfo.ip]) {
            console.log('设备未连接或设备信息不正确1')
        }
        let socket = this.clients[machineInfo.ip]
        socket && this.send(socket, content, async (key, arr, type) => {
            //预热炉头结果回复更新数据库
            let [error1, result1] = await to(HotpotPreFurnaceOrder.updateOne({ _preFurnaceOrderId, date: today }, { status: 1 }))
            if (error1) return 
            console.log('更新预热炉头状态成功,status= ' + 1 + ',_preFurnaceOrderId= ' + _preFurnaceOrderId)
        })
    }

    //插入打烊和超时数据, 触发打烊或超时清理库存
    async insertOutFoodOrder(order, type) {
        let insertData = []
        let data = order.data
        data.place.forEach(item => {
            insertData.push({
                date: today,
                orderDetail: {
                    thermotankNo: order.data.thermotank,
                    placeNo: item
                },
                type,
                status: 0
            })
        })
        let [error, result] = await to(HotpotOutFoodOrder.insertMany(insertData))
        if (error) return
        console.log('insert last _outFoodOrderId = ' + result[result.length - 1]._outFoodOrderId)

        //定时获取数据库打烊或超时的出餐指令,下发
        this.clearFood(type)
    }

    //打烊或超时清理库存
    clearFood(type) {
        setTimeout(async () => {
            let [error, result] = await to(HotpotOutFoodOrder.findOne({ date: today, type, status: 0 }))
            if (error) return
            if (!result) return console.log(type === 1 ? '打烊清库存完成' : '超时清库存完成')

            let machineInfo = this.machineInfos.find(item => item.header === '170')

            let socket = this.clients[machineInfo.ip]
            if (!socket) {
                console.log('设备未连接或设备信息不正确')
            } else {
                let { _outFoodOrderId, orderDetail } = result
                let content = this.messageResponse(170, 6, this.random, 6, _outFoodOrderId, orderDetail.thermotankNo, orderDetail.placeNo)
                this.send(socket, content, async (key, arr1, type) => {
                    if (![1, 2, 3, 4].includes(arr1[4])) console.log('未定义出餐结果类型,status=' + arr1[4], '_outFoodOrderId=' + _outFoodOrderId)

                    //出餐结果回复更新数据库
                    let [error2, result2] = await to(HotpotOutFoodOrder.updateOne({ _outFoodOrderId, date: today }, { status: arr1[4] }))
                    if (error2) return console.log(error2)
                    console.log('更新出餐状态成功,status= ' + arr1[4] + ',_outFoodOrderId= ' + _outFoodOrderId)

                    //保温库格子无餐,无法出餐,上报云端减少保温柜库存
                    if (arr1[4] === 4) {
                        console.log('保温库格子无餐,无法出餐,人工处理', '_outFoodOrderId=' + _outFoodOrderId)
                        return
                        // 暂时先不修改云端数据
                        let [err, res] = await to(reqchangeThermotankStock({ storeId: this.storeId, deviceId: this.deviceId, thermotankNo: orderDetail.thermotankNo, placeNo: orderDetail.placeNo, placeStock: 0 }))
                        if (err) return
                        console.log('保温库格子无餐,无法出餐,上报云端减少保温柜库存成功', '_outFoodOrderId=' + _outFoodOrderId)
                    }
                })
            }

            this.clearFood(type)

        }, 3000)
    }

    listenTcpData() {
        this.tcpConnect(socket => {
            this.clientConnected(socket);
            this.addSocket(socket)
        })
    }

    receive(key, arr, type) {
        let result = this.checkData(arr)
        if (result.code !== 0) return console.log(result.msg)
        // 客户端 1心跳 2设备异常状态上报 3设备状态上报 4是否备餐响应 5备餐完成 6是否可以出餐响应 7上报出餐结果 8是否可以放餐响应 9上报放餐结果 10炉头状态上报 11响应预热炉头 12炉头预热结果
        // 服务端 1响应心跳 2响应异常状态上报 3响应状态上报 4发起备餐 5响应备餐完成 6发起出餐 7响应出餐结果 8发起放餐 9响应放餐结果 10响应炉头状态上报 11发起预热炉头 12响应炉头预热结果

        if (this['command' + type]) {
            this.clients[key].send = (arr) => {
                this.clients[key].write(this.messageResponse(...arr), 'hex')
            }
            this['command' + type](this.clients[key], arr, this.devices)
        } else {
            let emitArr = [4, 6, 8, 11]
            if (emitArr.includes(type)) {
                if (!(event.eventNames().includes(arr[2] + ''))) return console.log('客户端响应了未下发的命令', arr)
                event.emit(arr[2], key, arr, type);
            } else {
                //发送未定义消息类型
                console.log('未定义消息类型')
            }
        }

    }

    //用户扫码
    async receiveScanData(scanData, key) {
        // console.log(key)
        return
        let qrCode = scanData.qrCode || '123'
        let calledMealId = scanData.calledMealId || '456'
        let exitNo = scanData.key === '::ffff:192.168.3.88' ? 1 : 2
        let [err, res] = await to(reqcheckQrCode({ storeId: this.storeId, deviceId: this.deviceId, qrCode, calledMealId, exitNo }))
        if (err) return
        if (res.status !== '200') return console.log(res.statusMsg)
        if (res.data.valid !== 'Y') return console.log(res.data.tip)  //Y可出餐,N不可出餐

        let [err1, res1] = await to(getThermotankTakePlace({ storeId: this.storeId, deviceId: this.deviceId, productId: res.data.productId }))
        if (err1) return
        if (res1.status !== '200') return console.log(res1.statusMsg)

        this.outFood(res1.data)
    }

    getFreeOrder(cb) {
        //定义的煲仔饭客户端的ip
        HotpotOrder.findOne({ status: 0 }, (error, result) => {
            if (error) return console.log(error)
            if (!result) return console.log('没有待处理的订单')
            cb(result)
        })
    }

    addSocket(socket) {
        this.machineInfos.forEach(machine => {
            if (machine.ip === socket.remoteAddress) {
                socket.lastTime = new Date().getTime()
                let key = socket.remoteAddress
                this.clients[key] && this.clearSocket(key)
                this.clients[key] = socket
                // this.devices[key] = { status: 0 } //0未发送状态 1已发送状态
                // this.thermotanks[key] = { status: 0 } //0未发送状态 1已发送状态
                console.log(key + ' 煲仔饭设备已连接')
            }
        })

    }

    clearSocket(key) {
        this.clients[key].destroy();
        delete this.clients[key];
        delete this.devices[key];
        delete this.thermotanks[key];
    }

    checkData(arr) {
        //头信息错误
        if (arr[0] !== 170) {
            return {
                code: 1,
                msg: '头信息错误'
            }
        }
        //数据长度错误
        if (arr[3] / 2 + 4 !== arr.length) {
            return {
                code: 2,
                msg: '数据长度错误'
            }
        }
        return {
            code: 0,
            msg: 'ok'
        }
    }

    checkHeartbeat() {
        setInterval(() => {
            let now = new Date().getTime();
            for (let key in this.clients) {
                if (now - this.clients[key].lastTime > 12000) {
                    this.clearSocket(key)
                    console.log(key + ' 超时下线')
                }
            }
        }, 12000 * 1000);
    }

    send(socket, content, cb) {
        let random = this.random
        event.once(random, cb)
        this.random++
        if (this.random > 65535) this.random = 1001
        socket.write(content, 'hex')
        // this.resendObj[random] = 1
        // this.resend(random, socket, content, cb)

    }

    resend(random, socket, content, cb) {
        setTimeout(() => {
            if (event.eventNames().includes(random + '')) {
                if (this.resendObj[random] < 100) {
                    this.resendObj[random] += 1
                    socket.write(content, 'hex')
                    this.resend(random, socket, content, cb)
                } else {
                    event.removeListener(random, cb)
                    delete this.resendObj[random]
                    console.log('命令超时未回复', content)
                }
            }
        }, 3000)
    }

    newDevicesStatus(arr) {
        return {
            freezerControlMode: arr[4],
            freezerWorkStatus: arr[5],
            freezerDeviceStatus: arr[6],
            thermotank1ControlMode: arr[7],
            thermotank1WorkStatus: arr[8],
            thermotank1DeviceStatus: arr[9],
            thermotank2ControlMode: arr[10],
            thermotank2WorkStatus: arr[11],
            thermotank2DeviceStatus: arr[12],
            outport1Status: arr[13],
            outport2Status: arr[14],
            inport1Status: arr[15],
            inport1MealId: arr[16],
            inport2Status: arr[17],
            inport2MealId: arr[18],
            warning1: arr[19],
            warning2: arr[20],
            status: 1
        }
    }

    responseMsg(arr) {
        return [arr[0], arr[1], arr[2], 0]
    }

    //下发备餐
    prepareFood() {
        setTimeout(() => {
            for (let key in this.clients) {
                let machineInfo = this.machineInfos.find(item => item.ip === key)
                if (!this.devices[machineInfo.header]) return
                //判断设备状态
                if (this.devices[machineInfo.header].freezerControlMode === 1 && this.devices[machineInfo.header].freezerWorkStatus === 1 && this.devices[machineInfo.header].freezerDeviceStatus === 1) {

                    this.getFreeOrder(result => {

                        //下发订单
                        const { productCode, deviceLevel } = result.orderDetail

                        let content = this.messageResponse(170, 4, this.random, 6, productCode, deviceLevel, productCode)

                        this.send(this.clients[key], content, (key1, arr, type) => {
                            let { _id } = result
                            switch (arr[4]) {
                                case 1:
                                    HotpotOrder.updateOne({ _id }, { status: 1 }, (error, result1) => {
                                        if (result1) {
                                            console.log('更新成功, _id: ' + _id)
                                        } else {
                                            console.log('更新失败, _id: ' + _id)
                                        }
                                        //通知云端减少库存
                                    })
                                    break;
                                case 2:
                                    console.log('执行失败，正在备餐中或者冷柜无库存, _id: ' + _id)
                                    break;
                                case 3:
                                    console.log('忙碌中，稍后重试, _id: ' + _id)
                                default:
                                    console.log('客户端返回是否可以制作的参数不正确, arr[4]: ' + arr[4] + ', _id: ' + _id)
                                    break;
                            }
                        })

                    })
                }
            }

        }, 5 * 1000)
    }

    //下发出餐
    async outFood(orderDetail) {
        //获取socket
        let machineInfo = this.machineInfos.find(item => item.header === '170')
        if (!this.clients[machineInfo.ip]) {
            return console.log('设备未连接或设备信息不正确')
        }
        let socket = this.clients[machineInfo.ip]

        let insertData = [{
            date: today,
            orderDetail,
            type: 0, //0 用户扫码出餐
            status: 0
        }]

        //发送出餐命令前先存入数据库
        let [error1, result1] = await to(HotpotOutFoodOrder.insertMany(insertData))
        if (error1) return console.log(error1)
        let _outFoodOrderId = result1[0]._outFoodOrderId
        let content = this.messageResponse(170, 6, this.random, 6, _outFoodOrderId, orderDetail.thermotankNo, orderDetail.placeNo)
        //发送出餐
        this.send(socket, content, async (key, arr1, type) => {
            // 0x0001：开始执行，0x0002：执行失败  0x0003忙碌，0x0004保温库格子无餐
            if (![1, 2, 3, 4].includes(arr1[4])) console.log('未定义出餐结果类型,status=' + arr1[4], '_outFoodOrderId=' + _outFoodOrderId)

            //出餐结果回复更新数据库
            let [error2, result2] = await to(HotpotOutFoodOrder.updateOne({ _outFoodOrderId, date: today }, { status: arr1[4] }))
            if (error2) return console.log(error2)
            console.log('更新出餐状态成功,status= ' + arr1[4] + ',_outFoodOrderId= ' + _outFoodOrderId)

            //保温库格子无餐,无法出餐,上报云端减少保温柜库存
            if (arr1[4] === 4) {
                console.log('保温库格子无餐,无法出餐,人工处理', '_outFoodOrderId=' + _outFoodOrderId)
                return
                // 暂时先不修改云端数据
                let [err, res] = await to(reqchangeThermotankStock({ storeId: this.storeId, deviceId: this.deviceId, thermotankNo: orderDetail.thermotankNo, placeNo: orderDetail.placeNo, placeStock: 0 }))
                if (err) return
                console.log('保温库格子无餐,无法出餐,上报云端减少保温柜库存成功', '_outFoodOrderId=' + _outFoodOrderId)
            }
        })
    }

    //下发放餐
    async inFood(inportId, machineId, socket) {
        //判断保温柜三个状态
        //如果正常,调接口获取放餐位置数据=>发起放餐
        let thermotankControlMode = 'thermotank' + inportId + 'ControlMode'
        let thermotankWorkStatus = 'thermotank' + inportId + 'WorkStatus'
        let thermotankDeviceStatus = 'thermotank' + inportId + 'DeviceStatus'
        if (this.devices[machineId][thermotankControlMode] === 1 && this.devices[machineId][thermotankWorkStatus] === 1 && this.devices[machineId][thermotankDeviceStatus] === 1) {
            //调接口获取放餐位置数据
            let res = {}
            res.data = inFoodObj
            let orderDetail = res.data
            //下发放餐

            let insertData = [{
                date: today,
                orderDetail,
                status: 0
            }]
            //发送放餐命令前先存入数据库
            let [error1, result1] = await to(HotpotInFoodOrder.insertMany(insertData))
            if (error1) return console.log(error1)


            let _inFoodOrderId = result1[0]._inFoodOrderId
            let content = this.messageResponse(170, 8, this.random, 6, _inFoodOrderId, orderDetail.thermotankNo, orderDetail.placeNo)
            //发送放餐
            this.send(socket, content, async (key, arr1, type) => {
                // 1：开始执行，2：执行失败  3: 忙碌，4: 放的格子有餐
                if (![1, 2, 3, 4].includes(arr1[4])) console.log('未定义放餐结果类型,status=' + arr1[4], '_inFoodOrderId=' + _inFoodOrderId)
                //放餐结果回复更新数据库
                let [error2, result2] = await to(HotpotInFoodOrder.updateOne({ _inFoodOrderId, date: today }, { status: arr1[4] }))
                if (error2) return console.log(error2)
                console.log('更新放餐状态成功,status= ' + arr1[4] + ',_inFoodOrderId= ' + _inFoodOrderId)

                //保温库格子有餐,无法放餐,上报云端增加保温柜库存
                if (arr1[4] === 4) {
                    console.log('保温库格子有餐,无法放餐,上报云端增加保温柜库存', '_inFoodOrderId=' + _inFoodOrderId)
                    return
                    // 暂时先不修改云端数据
                    let [err, res] = await to(reqchangeThermotankStock({ storeId: this.storeId, deviceId: this.deviceId, thermotankNo: orderDetail.thermotankNo, placeNo: orderDetail.placeNo, placeStock: 0 }))
                    if (err) return
                    console.log('保温库格子有餐,无法放餐,上报云端增加保温柜库存', '_inFoodOrderId=' + _inFoodOrderId)
                }
            })
        }
    }

    //心跳
    command1(socket, arr) {
        this.devices[arr[0]] = this.newDevicesStatus(arr)
        socket.send(this.responseMsg(arr))
    }

    //异常上报
    command2(socket, arr) {
        this.devices[arr[0]] = this.newDevicesStatus(arr)
        socket.send(this.responseMsg(arr))
    }

    //状态上报
    async command3(socket, arr) {
        this.devices[arr[0]] = this.newDevicesStatus(arr)
        socket.send(this.responseMsg(arr))
        //放餐口有菜
        if (this.devices[arr[0]].inport1Status === 1) {
            this.inFood(1, arr[0], socket)
        }
        if (this.devices[arr[0]].inport2Status === 1) {
            this.inFood(2, arr[0], socket)
        }
    }

    //备餐完成
    async command5(socket, arr) {
        // 任务号 arr[4]
        let [error, result] = await to(HotpotOrder.findOne({ date: today, _mealId: arr[4] }))
        if (error) return
        if (!result) return console.log('客户端发送的备餐任务号不存在arr[4]=' + arr[4], arr)
        if (result.status !== 1) return console.log('该订单号的状态为status=' + result.status, '不能处理')
        let [error1, result1] = await to(HotpotOrder.updateOne({ date: today, _mealId: arr[4] }, { status: 2 }))
        if (error1) return
        console.log('更新备餐任务成功,_mealId=' + arr[4], arr)
        socket.send(this.responseMsg(arr))

        let [err, res] = await to(reqchangeFreezerStock({ storeId: this.storeId, deviceId: this.deviceId, deviceLevel: result.orderDetail.deviceLevel, quantity: -1 }))
        if (err) return
        console.log(res)
        let [error2, result2] = await to(HotpotOrder.updateOne({ date: today, _mealId: arr[4] }, { status: 5 }))
        if (error2) return
        console.log('上报备餐后减少冷库库存成功,_mealId=' + arr[4], arr)
    }

    //出餐完成
    async command7(socket, arr) {
        // 任务号 arr[4]
        let [error, result] = await to(HotpotOutFoodOrder.findOne({ date: today, _outFoodOrderId: arr[4] }))
        if (error) return
        if (!result) return console.log('客户端发送的出餐任务号不存在arr[4]=' + arr[4], arr)
        let [error1, result1] = await to(HotpotOutFoodOrder.updateOne({ date: today, _outFoodOrderId: arr[4] }, { status: 5 }))
        if (error1) return
        console.log('更新出餐任务成功,_outFoodOrderId=' + arr[4], arr)
        socket.send(this.responseMsg(arr))

        let [err, res] = await to(reqchangeThermotankStock({ storeId: this.storeId, deviceId: this.deviceId, thermotankNo: result.orderDetail.thermotankNo, placeNo: result.orderDetail.placeNo, placeStock: 0 }))
        if (err) return
        console.log(res)
        let [error2, result2] = await to(HotpotOutFoodOrder.updateOne({ date: today, _outFoodOrderId: arr[4] }, { status: 6 }))
        if (error2) return
        console.log('更新出餐任务发送到云端成功,_outFoodOrderId=' + arr[4], arr)
    }

    //放餐完成
    async command9(socket, arr) {
        // 任务号 arr[4]
        let [error, result] = await to(HotpotInFoodOrder.findOne({ date: today, _inFoodOrderId: arr[4] }))
        if (error) return
        if (!result) return console.log('客户端发送的放餐任务号不存在arr[4]=' + arr[4], arr)
        let [error1, result1] = await to(HotpotInFoodOrder.updateOne({ date: today, _inFoodOrderId: arr[4] }, { status: 5 }))
        if (error1) return
        console.log('更新放餐任务成功,_inFoodOrderId=' + arr[4], arr)
        socket.send(this.responseMsg(arr))

        let [err, res] = await to(reqchangeThermotankStock({ storeId: this.storeId, deviceId: this.deviceId, thermotankNo: result.orderDetail.thermotankNo, placeNo: result.orderDetail.placeNo, placeStock: 1 }))
        if (err) return
        console.log(res)
        let [error2, result2] = await to(HotpotInFoodOrder.updateOne({ date: today, _inFoodOrderId: arr[4] }, { status: 6 }))
        if (error2) return
        console.log('更新放餐任务发送到云端成功,_inFoodOrderId=' + arr[4], arr)

    }

    //预热炉头完成
    async command12(socket, arr) {
        // 任务号 arr[4]
        let [error, result] = await to(HotpotPreFurnaceOrder.findOne({ date: today, _preFurnaceOrderId: arr[4] }))
        if (error) return
        if (!result) return console.log('客户端发送的预热炉头任务号不存在arr[4]=' + arr[4], arr)
        let [error1, result1] = await to(HotpotPreFurnaceOrder.updateOne({ date: today, _preFurnaceOrderId: arr[4] }, { status: 2 }))
        if (error1) return
        console.log('更新预热炉头任务成功,_preFurnaceOrderId=' + arr[4], arr)
        socket.send(this.responseMsg(arr))
    }

}
module.exports = hotPotRice;


// 心跳	aa0001000000220001000100010001000100010001000100010001000100010001000100010001000100
// 异常上报	aa0002000000220001000100010001000100010001000100010001000100010001000100010001000100
// 状态上报	aa0003000000220001000100010001000100010001000100010001000100010001000100010001000100

// 备餐完成 aa00 0500 0000 0200 0100
// 出餐完成 aa00 0700 0000 0200 0100
// 放餐完成 aa00 0900 0000 0200 0100
// 预热炉头完成 aa00 0c00 0000 0200 0100

// 响应预热命令 aa000b00xxxx0000

//备餐完成上报库存
// aa0003000000220001000100010001000100010001000100010001000100010001000100010001000100
// aa000400ea0302000100
// aa00 0500 0000 0200 0100

//放餐完成上报库存
// aa0003000000220001000100010001000100010001000100010001000100010001000100010001000100
// aa00 0800 E903 0200 0100
// aa000900000002000100

//出餐完成上报库存
// aa0003000000220001000100010001000100010001000100010001000100010001000100010001000100
// aa000600e90302000100
// aa000600e90302000100
